package com.passer.web.travel.controller;

import java.util.List;

import com.passer.web.travel.service.ITImagesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passer.common.annotation.Log;
import com.passer.common.enums.BusinessType;
import com.passer.web.travel.domain.TJing;
import com.passer.web.travel.service.ITJingService;
import com.passer.common.core.controller.BaseController;
import com.passer.common.core.domain.AjaxResult;
import com.passer.common.utils.poi.ExcelUtil;
import com.passer.common.core.page.TableDataInfo;

/**
 * 必玩景点Controller
 *
 * @author passer
 * @date 2021-11-23
 */
@Controller
@RequestMapping("/travel/jing")
public class TJingController extends BaseController
{
    private String prefix = "travel/jing";

    @Autowired
    private ITJingService tJingService;

    @Autowired
    private ITImagesService tImagesService;

    @RequiresPermissions("travel:jing:view")
    @GetMapping()
    public String jing()
    {
        return prefix + "/jing";
    }

    /**
     * 查询必玩景点列表
     */
    @RequiresPermissions("travel:jing:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TJing tJing)
    {
        startPage();
        List<TJing> list = tJingService.selectTJingList(tJing);
        return getDataTable(list);
    }

    /**
     * 导出必玩景点列表
     */
    @RequiresPermissions("travel:jing:export")
    @Log(title = "必玩景点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TJing tJing)
    {
        List<TJing> list = tJingService.selectTJingList(tJing);
        ExcelUtil<TJing> util = new ExcelUtil<TJing>(TJing.class);
        return util.exportExcel(list, "必玩景点数据");
    }

    /**
     * 新增必玩景点
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存必玩景点
     */
    @RequiresPermissions("travel:jing:add")
    @Log(title = "必玩景点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TJing tJing)
    {
        int i = tJingService.insertTJing(tJing);
        //插入封面图片到图片库中
        tImagesService.saveCover(tJing);
        return toAjax(i);
    }

    /**
     * 修改必玩景点
     */
    @GetMapping("/edit/{jid}")
    public String edit(@PathVariable("jid") Long jid, ModelMap mmap)
    {
        TJing tJing = tJingService.selectTJingById(jid);
        tJing.setCoverURL(tImagesService.selectCoverByBusiness(tJing).getSrc());
        mmap.put("tJing", tJing);
        return prefix + "/edit";
    }

    /**
     * 修改保存必玩景点
     */
    @RequiresPermissions("travel:jing:edit")
    @Log(title = "必玩景点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TJing tJing)
    {
        //插入封面图片到图片库中
        int i = tJingService.updateTJing(tJing);
        tImagesService.saveCover(tJing);
        return toAjax(i);
    }

    /**
     * 删除必玩景点
     */
    @RequiresPermissions("travel:jing:remove")
    @Log(title = "必玩景点", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tJingService.deleteTJingByIds(ids));
    }
}
